package io.kqoa.modules.kq.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class TimeUtils {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    // 获得当前月--开始日期
    public static String getMinMonthDate(String date) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(dateFormat.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 比较2个时间的时分秒 成功返回成功
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static int compareHoursMinutesSeconds(String startTime, String endTime) {
        try {
            DateFormat df = new SimpleDateFormat("HH:mm:ss");
            Date dt1 = df.parse(startTime);//将字符串转换为date类型
            Date dt2 = df.parse(endTime);//现在时间
            if (dt2.getTime()>dt1.getTime()){
                return 1;
            }
            return 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    // 获得当前月--结束日期
    public static String getMaxMonthDate(String date){
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(dateFormat.parse(date));
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return dateFormat.format(calendar.getTime());
        }  catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    获取每一个月开始的时间和结束时间 endTime  结束的时间
     * @param startTime
     */
    public static List<Date> getTime() throws ParseException {
        List<Date> stringList=new ArrayList<>();
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(calendar.getTime());
        String minMonthDate = getMinMonthDate(format);
        //开始的时间
        String startTime = minMonthDate+" 00:00:00";
        SimpleDateFormat dateForm= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = dateForm.parse(startTime);
        stringList.add(date);
        //结束的时间
        String maxMonthDate = getMaxMonthDate(format);
        String endTime = maxMonthDate+" 59:59:59";
        Date dat= dateForm.parse(endTime);
        stringList.add(dat);
        return stringList;
    }
    public static Date getSpecifiedTime(String dateTime)  {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);
        log.info(dateNowStr);
        String startTime = dateNowStr+" "+dateTime;
        log.info(dateTime);
        SimpleDateFormat dateForm= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;
        try {
            date = dateForm.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据日期获取周几
     * @param dateAttendanceStatus
     * @return
     */
    public static String getWeekday(Date dateAttendanceStatus) {// 必须yyyy-MM-dd
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        String date=  sd.format(dateAttendanceStatus);
        SimpleDateFormat sdw = new SimpleDateFormat("E");
        Date d = null;
        try {
            d = sd.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdw.format(d);
    }
    public static Date  getDate(Date date){
        SimpleDateFormat sd = new SimpleDateFormat("MM-dd");
        String date1=  sd.format(date);
        Date parse=null;
        try {
           parse = sd.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取时间的月份
     * @param
     */
    public static int queryMonthAcquisitionTime(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int month = ca.get(Calendar.MONTH)+ 1;//第几个月
        return month;

    }

    /**
     * 获取当前日期的前3个月时间
     * @param
     * @return
     */
    public static Date queryGetFirstMonths(){
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dNow);
        calendar.add(Calendar.MONTH, -2);//得到前3个月
        Date  formNowthreeMonth = calendar.getTime();

        return formNowthreeMonth;
    }

    public static void main(String[] args) throws ParseException {

//

    }


    public static long get_d_plaus_1(Calendar c) {
        //day_of_month
        c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
        return c.getTimeInMillis();
    }

    /**
     * 根据年和月获取这个月开始的时间
     * @param year
     * @param month
     * @return
     */
    public static Date getBeginTime(int year, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate localDate = yearMonth.atDay(1);
        LocalDateTime startOfDay = localDate.atStartOfDay();
        ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));

        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 根据年和月获取这个月结束的时间
     * @param year
     * @param month
     * @return
     */

    public static Date getEndTime(int year, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        YearMonth yearMonth = YearMonth.of(year, month);
        LocalDate endOfMonth = yearMonth.atEndOfMonth();
        LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
        return Date.from(zonedDateTime.toInstant());
    }
    /**
     * 根据月获取开始和结束的时间
     */
     public static List<String>  queryStartTimesMonth(int month){
         List<String> dateList=new ArrayList<>();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
         Date date = new Date();
         //获取当前年份
         String year=sdf1.format(date);
         log.info("当前的年份是========="+year);
         Date beginTime = getBeginTime(Integer.parseInt(year), month);
         System.out.println("开始的时间"+sdf.format(beginTime));
         dateList.add(sdf.format(beginTime));
         Date endTime = getEndTime(Integer.parseInt(year), month);
         System.out.println("结束的时间"+sdf.format(endTime));
         dateList.add(sdf.format(endTime));
         return dateList;
     }
    /**
     * 获取时间的日
     */
    public  static  int GetsDayTime(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DATE);
        return day;
    }
    /**
     * 将字符串转成data
     */
    public static Date convertStringData(String dataString){
        Date date=null;
        try {
           date = new SimpleDateFormat("yyyy-MM-dd").parse(dataString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
